const api = require("../../../utils/api")
const getwxphone = require("../../../utils/getwxphone")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    act: 11,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    // wx.setStorageSync('53035phone', '15205451183')
    wx.login({
      success: (res) => {
        var code = res.code;
        this.setData({
          code
        })
      }
    })
    var q1 = wx.getStorageSync('q1')
    var q2 = wx.getStorageSync('q2')
    var q3 = wx.getStorageSync('q3')
    var q4 = wx.getStorageSync('q4')
    var q11 = wx.getStorageSync('q11')
    var tip = wx.getStorageSync('tip')
    console.log(options);
    _.setData({
      q1,
      q2,
      q3,
      q4,
      q11,
      tip
    })
    api.load(options)
  },
  close(e) {
    var _ = this;
    wx.setStorageSync('tip', '1')
    _.setData({
      tip: 1
    })
  },
  add(e) {
    var _ = this;
    var id = e.currentTarget.dataset.id;
    var phone = wx.getStorageSync('53035phone')
    if (phone) {
      if (id == '1') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送公告发布时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q1', '1')
              _.setData({
                q1: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['IDSJ5gZMvZGGvSRLPxKCKx02WW1TiXzFIWqSUEKmBzE'], //笔试时间
                success(res) {
                  console.log(res);
                  wx.request({
                    url: 'https://zg99.offcn.com/index/biaodan/updateuser?actid=53035',
                    method: 'get',
                    data: {
                      ggfb: 1,
                      sstimes: Date.parse(new Date()),
                      phone: wx.getStorageSync('53035phone')
                    },
                    success(res) {
                      api.showToast('预约成功！')
                    }
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '2') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送国考报名时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q2', '1')
              _.setData({
                q2: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['GFqJYmcnvqp_uIjIFLtZFo9az1HnXr-JKmbwdV-C1tE'], //笔试时间
                success(res) {
                  console.log(res);
                  wx.request({
                    url: 'https://zg99.offcn.com/index/biaodan/updateuser?actid=53035',
                    method: 'get',
                    data: {
                      gkbm: 1,
                      sstimes: Date.parse(new Date()),
                      phone: wx.getStorageSync('53035phone')
                    },
                    success(res) {
                      api.showToast('预约成功！')
                    }
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '3') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送缴费提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q3', '1')
              _.setData({
                q3: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['DVOiaM6AvncG_ctQLN3X4kP26KhrMrvNO795-TC4oj0'], 
                success(res) {
                  console.log(res);
                  wx.request({
                    url: 'https://zg99.offcn.com/index/biaodan/updateuser?actid=53035',
                    method: 'get',
                    data: {
                      jftx: 1,
                      sstimes: Date.parse(new Date()),
                      phone: wx.getStorageSync('53035phone')
                    },
                    success(res) {
                      api.showToast('预约成功！')
                    }
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '4') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送笔试时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q4', '1')
              _.setData({
                q4: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['FHuriu5KRom3hI3gXHA5l4VnxjVZQAkosynrv9m2nl8'], //笔试时间
                success(res) {
                  console.log(res);
                  wx.request({
                    url: 'https://zg99.offcn.com/index/biaodan/updateuser?actid=53035',
                    method: 'get',
                    data: {
                      gkbs: 1,
                      sstimes: Date.parse(new Date()),
                      phone: wx.getStorageSync('53035phone')
                    },
                    success(res) {
                      api.showToast('预约成功！')
                    }
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '11') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送资格审查提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q11', '1')
              _.setData({
                q11: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['tzpnx2tykHOYtbJXudRd-FkqIfswgceDlYN2VgBffYE'], //笔试时间
                success(res) {
                  console.log(res);
                  wx.request({
                    url: 'https://zg99.offcn.com/index/biaodan/updateuser?actid=53035',
                    method: 'get',
                    data: {
                      zgsc: 1,
                      sstimes: Date.parse(new Date()),
                      phone: wx.getStorageSync('53035phone')
                    },
                    success(res) {
                      api.showToast('预约成功！')
                    }
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '22') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送缴费提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q22', '1')
              _.setData({
                q22: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['DVOiaM6AvncG_ctQLN3X4kP26KhrMrvNO795-TC4oj0'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '缴费22',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '是否需要推送缴费提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q22', '1')
              _.setData({
                q22: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['DVOiaM6AvncG_ctQLN3X4kP26KhrMrvNO795-TC4oj0'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '缴费22',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }


    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  getPhoneNumber(e) {
    var _ = this;
    const info = {
      area: wx.getStorageSync('area'),
      agent: wx.getStorageSync('agent'),
      page: wx.getStorageSync('page'),
      activeId: 42
    }

    getwxphone(e.detail.code, 53035, 'biaodan', info)
      .then(res => {
        console.log(res);
        wx.request({
          url: 'https://zg99.offcn.com/index/wechat/getsesionkey?actid=53035',
          method: 'get',
          data: {
            code: this.data.code
          },
          success(res) {
            console.log(res)
            if (res.data.openid) {
              wx.request({
                url: 'https://zg99.offcn.com/index/biaodan/updateuser?actid=53035',
                method: 'get',
                data: {
                  oid: res.data.openid,
                  sstimes: Date.parse(new Date()),
                  phone: wx.getStorageSync('53035phone')
                },
                success(res) {
                  _.setData({
                    login: !1
                  })
                }
              })
            } else {
              api.showToastNone('系统错误！')
            }

          }
        })

      })
      .catch(err => {
        console.log(err);
      })
  },
  litap(e) {
    var _ = this;
    console.log(e.currentTarget.dataset.act)
    var act = e.currentTarget.dataset.act;
    _.setData({
      act
    })
  },
  choiseday(e) {
    var _ = this;
    console.log(e.currentTarget.dataset.day)
    var day = e.currentTarget.dataset.day;
    _.setData({
      day
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _ = this;
    var area = wx.getStorageSync('area')
    var agent = wx.getStorageSync('agent')
    var page = wx.getStorageSync('page')
    return {
      title: '2023山东省省考百宝箱',
      path: 'pages/sk/blyy/blyy?area=' + area + '&agent=' + agent + '&page=' + page
    }

  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    var _ = this;
    var area = wx.getStorageSync('area')
    var agent = wx.getStorageSync('agent')
    var page = wx.getStorageSync('page')
    return {
      title: '2023山东省省考百宝箱',
      path: 'pages/sk/blyy/blyy?area=' + area + '&agent=' + agent + '&page=' + page
    }
  }

})